
export default function (G6){
    G6.registerBehavior("dragNode", {
        getDefaultCfg(){
            return {
                updateEdge: true,
                delegate: true,
                delegateStyle: {},
                align: true
            }
        },

        getEvents(){
            return {
                'node:dragstart': 'onDragStart',
                'node:drag': 'onDrag',
                'node:dragend':'onDragEnd'
            }
        },

        onDragStart(e) {
            if (!this.shouldBegin.call(this, e)){
                return;
            }
            this.target = e.item;
            this.node = e.item;
            this.origin = {
                x: e.x,
                y: e.y,
            }
        },

        onDrag(e) {
            // if(e.item.getModel().clazz === 'map'){
            //     return;
            // }

            
            if (!this.origin) {
                return;
            }
            if (!this.get('shouldUpdate').call(this, e)){
                return;
            }

            const origin = this.origin;
            const model = this.target.get('model');

            if (!this.point) {
                this.point = {
                    x: model.x,
                    y: model.y
                }
            }
            const x = e.x - origin.x + this.point.x;
            const y = e.y - origin.y + this.point.y;
            this.origin = {x:e.x, y: e.y};
            this.point = {x, y};
            if(this.delegate){
                this._updateDelegate(this.target, x, y);
            }
        },

        onDragEnd(e) {
            const delegateShape = e.item.get('delegateShape');
            if (!this.origin) {
                return;
            }
            if (!this.get('shouldUpdate').call(this, e)){
                return;
            }
            if(delegateShape){
                const bbox = delegateShape.getBBox();
                const x = parseInt(bbox.x + bbox.width / 2);
                const y = parseInt(bbox.y + bbox.height / 2);
                const moveX = x - this.node.getModel().x;
                const moveY = y - this.node.getModel().y;
                delegateShape.remove();
                this.target.set('delegateShape', null);
                this._updateItem(this.target, {x, y});
                if(e.item.getModel().clazz === 'map'){
                    this.graph.getNodes().filter((item)=>item.getModel().clazz != e.item.getModel().clazz).forEach(element => {
                        const xx = element.getModel().x + moveX;
                        const yy = element.getModel().y + moveY;
                        this._updateItem(element, {x: parseInt(xx), y: parseInt(yy)});
                    });
                }
            }
            this.point = null;
            this.origin = null;
            this.graph.emit('afternodedragend');
        },

        _updateItem(item, point){
            if (this.graph.executeCommand) {
                this.graph.executeCommand('update', {
                    itemId: item.get('id'),
                    updateModel: point
                })
            }else {
                if(this.get('updateEdge')){
                    this.graph.updateItem(item, point);
                } else {
                    item.updatePosition(point);
                }
            }
        },

        _updateDelegate(item, x, y){
            const self = this;
            let shape = item.get('delegateShape');
            const bbox = item.get('keyShape').getBBox();
            let moveX =  x - bbox.width /2;
            let moveY =  y - bbox.height /2;
            if(!shape) {
                const parent = self.graph.get('group');
                const attrs = {
                    stroke: '#1890ff',
                    fill: '#1890ff',
                    fillOpacity: 0.08,
                    lineDash: [4, 4],
                    radius: 4,
                    lineWidth: 1
                }
                shape = parent.addShape('rect', {
                    attrs: {
                        width: bbox.width,
                        height: bbox.height,
                        x: moveX,
                        y: moveY,
                        nodeId: item.get("id"),
                        ...attrs
                    }
                });
                shape.set('capture', false);
                item.set('delegateShape', shape);
            }
            shape.attr({x: moveX, y: moveY});
            this.graph.paint();
            // this.graph.emit('afternodedrag', shape);
        }
    })
}
